﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace ScheduleManagement.DAL
{
    /// <summary>
    /// 数据访问层_用户信息操作类（增、删除、改）
    /// </summary>
    public class User_Dal
    {
        //读取配置文件,配置数据库连接字符串
        string cn = Properties.Settings.Default.ConnectionString;

        /// <summary>
        /// 添加一个新用户
        /// </summary>
        /// <param name="user">用户信息实体要设置U_Name,U_Password两个属性值</param>
        public bool AddUser()
        {
            string commandText = "p_adduser";
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@u_Name", ScheduleManagement.MODEL.Users.U_Name);
            commandParameters[1] = new SqlParameter("@u_Password", ScheduleManagement.MODEL.Users.U_Password);
            SqlHelper.ExecuteNonQuery(cn,commandText,commandParameters);
            return true;
        }

        /// <summary>
        /// 检查用户是否合法并返回用户权限
        /// </summary>
        /// <param name="userinfo"></param>
        /// <returns></returns>
        public int CheckUser()
        {
            string commandText = "p_checkuser";
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@u_Name", ScheduleManagement.MODEL.Users.U_Name);
            commandParameters[1] = new SqlParameter("@u_Password", ScheduleManagement.MODEL.Users.U_Password);
            commandParameters[2] = new SqlParameter("@ch_Id", SqlDbType.Int, 1);
            commandParameters[2].Direction = ParameterDirection.Output;
            int flag = Convert.ToInt32(SqlHelper.ExecuteScalar(cn, CommandType.StoredProcedure, commandText, commandParameters));
            if (flag != 0)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(commandParameters[2].Value);
            }

        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userinfo"></param>
        public void DelUser()
        {
            string commandText = "p_deluser";
            SqlParameter[] commandParameters = new SqlParameter[1];
            commandParameters[0] = new SqlParameter("@u_Name",ScheduleManagement.MODEL.Users.U_Name);
            SqlHelper.ExecuteNonQuery(cn, CommandType.StoredProcedure, commandText,commandParameters);            
        }

        /// <summary>
        /// 查询管理员管理密码
        /// </summary>
        /// <param name="admininfo"></param>
        /// <returns></returns>
        public string CheckAdmin()
        {
            string commandText = "p_checkAdmin";
            SqlParameter[] commandParameters = new SqlParameter[1];
            commandParameters[0] = new SqlParameter("@u_Name", ScheduleManagement.MODEL.Users.U_Name);
            string psw =SqlHelper.ExecuteScalar(cn, CommandType.StoredProcedure, commandText, commandParameters).ToString();
            return psw;
        }

        /// <summary>
        /// 设置新用户管理员权限
        /// </summary>
        /// <param name="usrename"></param>
        public void ChangeAdmin(string username)
        {
            string commandText = "p_changeAdmin";
            SqlParameter[] commandParameters = new SqlParameter[1];
            commandParameters[0] = new SqlParameter("@u_Name", username);
            SqlHelper.ExecuteNonQuery(cn, CommandType.StoredProcedure, commandText, commandParameters).ToString();
        }
    }
}
